#include "mainwindow.h"
#include <QMenuBar>
#include <QtWidgets>
#include <QDebug>
#include "setclient.h"
#include "clientlist.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
      s_EC(ExperimentConfiguration::get())
{
    resize(1024 , 768);
    createMenus();
    QWidget *widget = new QWidget();
    this->setCentralWidget(widget);
    QGridLayout *total = Initialization();
    widget->setLayout(total);

}

MainWindow::~MainWindow()
{
}

void MainWindow::createMenus()
{
    QMenu *testMenu = menuBar()->addMenu(tr("&功能"));
    QAction *addClientAct = new QAction(tr("&新增客户端"));
    QAction *removeClientAct = new QAction(tr("&删除客户端"));
    QAction *exitAct = new QAction(tr("&退出"), this);

    testMenu->addAction(addClientAct);
    testMenu->addAction(removeClientAct);
    testMenu->addSeparator();
    testMenu->addAction(exitAct);

    connect(addClientAct, &QAction::triggered, this, &MainWindow::addClient);
    connect(exitAct, &QAction::triggered, this, &QWidget::close);


    QMenu *helpMenu = menuBar()->addMenu(tr("&帮助"));
    QAction *aboutAct = new QAction(tr("&关于"));
    helpMenu->addAction(aboutAct);

}
QGridLayout * MainWindow::Initialization()
{
    QLabel* areaLabel = new QLabel(tr("控制平台"));
    QFont areaFont;
    areaFont.setFamily("楷体");
    areaFont.setBold(true);
    areaFont.setPointSize(36);
    areaFont.setLetterSpacing(QFont::PercentageSpacing,200);
    areaLabel->setAlignment(Qt::AlignCenter);
    areaLabel->setFont(areaFont);
    areaLabel->setStyleSheet("color:#f9a263;");

    //QLabel* dateLabel = new QLabel(tr("客户端显示区域，格式待定"));
    clientViewListWidget =new ClientList(this);
    clientViewListWidget->clear();
    //clientViewListWidget->initPiece();
    QPushButton * addButton = new QPushButton(tr("新增客户机"));
    QPushButton * removeButton = new QPushButton(tr("删除客户机"));

    connect(addButton, &QPushButton::clicked, this, &MainWindow::addClient);
    QDialogButtonBox* areaButtonBox = new QDialogButtonBox(Qt::Horizontal);
    areaButtonBox->addButton(addButton, QDialogButtonBox::ActionRole);
    areaButtonBox->addButton(removeButton, QDialogButtonBox::ActionRole);

    // Set Layout
    QGridLayout* layout = new QGridLayout;
    layout->addWidget(areaLabel, 0, 0);
    layout->addWidget(clientViewListWidget, 1, 0);
    layout->addWidget(areaButtonBox, 2, 0);

    return layout;
}

void MainWindow::addClient()
{

    SetClient *dialog = new SetClient(this);
    dialog->exec();
    //std::shared_ptr<ClientInformation> addClientInformation(new ClientInformation);
    //addClientInformation->ipAddr = s_EC.clientIpList.at(0);
    //s_EC.clientStaticList.append(addClientInformation);
    ExperimentConfiguration::clientStatic addClient;
    addClient.clientIp = s_EC.clientIp;
    addClient.clientInformation = s_EC.clientInfor;
    addClient.tcpServer = s_EC.tcpServer;
    s_EC.clientStaticList.append(addClient);
    clientViewListWidget->addPiece(addClient);

    //s_EC.clientIpList.remove(0);

    return;
}
